package com.bdqn.zhang.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Author: tao
 * @Description: talk is cheap,show me the code!
 * @Date: create in 2020/9/12 17:21
 */
public class MyBatisUtil {

    static SqlSessionFactory sqlSessionFactory = null;

    static {
        String str = "mybatis_config.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(str);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (Exception e){

        }
    }

    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }

    public static void closeSession(SqlSession session){
        if(session != null){
            session.close();
        }
    }
}
